from django.contrib.auth.hashers import make_password, check_password
from django.db import IntegrityError
from django.http import JsonResponse
from rest_framework.views import APIView
from user.serializer import UserSerializer, FriendSerializer, FileSerializer
from user.models import Account, Friends, Files
from utils.custom_viewset_base import CustomViewBase
from django.db.models import Q


class Register(APIView):

    def get(self, request):
        username = request.GET.get('username', None)
        if username is None:
            return JsonResponse({'code': 1001, 'message': '请传入参数username的值'})
        else:
            user_username = Account.objects.filter(username=username)
            if user_username:
                return JsonResponse({'code': 1001, 'message': '账号已存在'})
            else:
                return JsonResponse({'code': 2000, 'message': '账号可用'})
        # email = request.GET.get('email', None)
        # user_email = Account.objects.filter(email=email)
        # if user_email:
        #     return JsonResponse({'code': '1006', 'message': '邮箱已存在'})

    def post(self, request):
        try:
            username = request.data.get('username', None)
            nickname = request.data.get('nickname', None)
            password = request.data.get('password', None)
            sex = request.data.get('sex', None)
            email = request.data.get('email', None)
            # phone = request.POST.get('phone', None)
            # birth = request.POST.get('birth', None)
            if username is None:
                return JsonResponse({'code': 1001, 'message': '账号不能为空'})
            if nickname is None:
                return JsonResponse({'code': 1001, 'message': '昵称不能为空'})
            if email is None:
                return JsonResponse({'code': 1001, 'message': '邮箱不能为空'})
            if password is None:
                return JsonResponse({'code': 1001, 'message': '密码不能为空'})
            # if not phone:
            #     return JsonResponse({'code': '1004', 'message': '电话为空'})
            password = make_password(password)
            user, b = Account.objects.get_or_create(username=username,nickname=nickname, password=password, email=email,sex=sex)
            if b:
                return JsonResponse({'code': 2000, 'message': '注册成功'})
            else:
                return JsonResponse({'code': 1001, 'message': '账号已存在'})
        except IntegrityError:
            return JsonResponse({'code': 1001, 'message': '账号或邮箱已存在'})
        except Exception as e:
            print(e)
            print(type(e))
            return JsonResponse({'code': 1001, 'message': '注册失败'})


class Login(APIView):
    def post(self, request):
        try:
            username = request.data.get('username', None)
            password = request.data.get('password', None)
            if username is None:
                return JsonResponse({'code': 1001, 'message': '账号不能为空'})
            if password is None:
                return JsonResponse({'code': 1001, 'message': '密码不能为空'})
            user = Account.objects.filter(username=username).first()
            password_result = check_password(password, user.password)
            if password_result:
                data = {
                    'id':user.id,
                    'userName':user.username,
                    'nickName':user.nickname,
                    'avatar':user.avatar
                }
                return JsonResponse({'code': 2000, 'message': '登录成功','data':data})
            else:
                return JsonResponse({'code': 1001, 'message': '密码错误'})
        except Account.DoesNotExist:
            return JsonResponse({'code': 1001, 'message': '账号错误'})
        except Exception as e:
            print(e)
            print(type(e))
            return JsonResponse({'code': 1001, 'message': '账号或密码错误'})


class UsersInfoView(CustomViewBase):
    """
    用户信息
    """
    queryset = Account.objects.all()
    serializer_class = UserSerializer

class FriendListView(CustomViewBase):
    """
    好友列表
    """
    queryset = Friends.objects.all()
    serializer_class = FriendSerializer

    def get_queryset(self):
        params = self.request.query_params
        #print(Friends.objects.filter(Q(user_id=params.get('user_id')) | Q(friend_id=params.get('user_id'))).all())
        if params.get('user_id') is not None:
            return Friends.objects.filter(Q(user_id=params.get('user_id')) | Q(friend_id=params.get('user_id'))).all()
        else:
            return Friends.objects.all()


class FilesView(CustomViewBase):
    """
    文件管理
    """
    queryset = Files.objects.all()
    serializer_class = FileSerializer